package com.hsy.baoinbao.modules.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.deparse.dpframe.ui.fragment.DPBaseFragment;
import com.deparse.dpframe.utils.DPGsonUtil;
import com.deparse.dpframe.utils.DPHttpUtil;
import com.deparse.dpframe.utils.DPLogUtil;
import com.hsy.baoinbao.R;
import com.hsy.baoinbao.common.Consts;
import com.hsy.baoinbao.modules.common.CommonBaseFragment;
import com.hsy.baoinbao.modules.login.LoginActivity;
import com.hsy.baoinbao.modules.main.ui.activity.MainActivity;
import com.hsy.baoinbao.modules.my.activity.AboutUsActivity;
import com.hsy.baoinbao.modules.my.activity.ChangePhoneActivity;
import com.hsy.baoinbao.modules.my.activity.ContactCustomerActivity;
import com.hsy.baoinbao.modules.my.gsondata.PersonalData;
import com.hsy.baoinbao.modules.my.view.CommonPersonRelativeLayout;
import com.hsy.baoinbao.utils.CommonUtils;
import com.hsy.baoinbao.utils.HttpUtil;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by hsy on 2016/3/23.
 */
public class FragmentMy extends CommonBaseFragment {
    private CommonPersonRelativeLayout person_change_phone, person_contact_customer, person_about_us;
    private CommonPersonRelativeLayout person_log_out;
    private ImageView person_head;
    private TextView person_name;
    private DPHttpUtil.HttpCallback callback = new DPHttpUtil.HttpCallback() {
        @Override
        public void onSuccess(int what, String response) {
            if (0 == what) {
                setPersonalData(response);
            }
        }

        @Override
        public void onError(int what, int type, VolleyError error) {
            CommonUtils.hideProgressDialog();
        }
    };

    @Override
    protected int getFragmentLayout() {
        return R.layout.fragment_my;
    }

    private void setPersonalData(String response) {
        PersonalData.Result result = DPGsonUtil.fromJson(response, PersonalData.Result.class);
        Picasso.with(getAttchedActivity()).load(Consts.API_IMAGE_FOLDER_MIDDLE + result.getHead_img()).into(person_head);
        person_name.setText(result.getNickname());

    }

    @Override
    protected void initView(View view, Bundle savedInstanceState) {

        person_head = findViewById(R.id.person_head);

        person_name = findViewById(R.id.person_name);

        person_change_phone = findViewById(R.id.person_change_phone);

        person_contact_customer = findViewById(R.id.person_contact_customer);

        person_about_us = findViewById(R.id.person_about_us);

        person_log_out = findViewById(R.id.person_log_out);

        person_change_phone.setTarget(ChangePhoneActivity.class);
        person_contact_customer.setTarget(ContactCustomerActivity.class);
        person_about_us.setTarget(AboutUsActivity.class);
        person_log_out.setDo(true, getAttchedActivity());

        Map<String, String> params = new HashMap<>();
        params.put(Consts.TOKEN, CommonUtils.getToken(getAttchedActivity()));
        HttpUtil.httpPostWithParams(getAttchedActivity(), 0, Consts.API_GET_PERSONAL_DATA, params, callback);
    }

    @Override
    protected void initData() {

//        sendEmptyMessage(0);
//        Message msg = Message.obtain();
//        msg.what = 0x8003;
//        msg.obj = getRootView();
//        sendMessage(msg);
//        sendEmptyMessage(1);
    }

    @Override
    protected void onHandleMessage(Message msg) {
//        if (0 == msg.what) {
//
//        } else if (0x8003 == msg.what) {
//            View view = (View) msg.obj;
//        } else if (1 == msg.what) {
//
//        }
    }

}
